﻿using com.guojin.mj.net.message.game;
using com.guojin.mj.net.message.login;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class GameEndPage : EventManager {

    public GameObject MVPImg;
    public GameObject daYuImg;
    public GameObject tuHaoImg;
    public GameObject playerImg;
    public Text playerScore;
    public Text ChapterCount;
    public Text MaxPot;
    public Text TotalTakeIn;
    public GameObject playerGroup;
    public Text bigCardType;
    public Button CloseBtn;

    public override void InformationSetting()
    {

    }

    public void Init(DissolveRoomRet dr)
    {
        AddListenerToButton(CloseBtn, GoToGameHallClick);
        SetText(ChapterCount, dr.totalChapter.ToString());
        SetText(MaxPot, dr.maxPot.ToString());
        SetText(TotalTakeIn, dr.totalTakeIn.ToString());
        SetText(bigCardType, dr.bigCardType);

        LoadGameEndItem(dr);
    }

    private void LoadGameEndItem(DissolveRoomRet dr)
    {
            var results = from item in dr.delRoomPlayerList orderby item.settleScore descending select item;

            foreach (var item in results)
            {
                //显示玩家头像
                if (GameData.GetInstance().userID == item.userId.ToString())
                {
                    CheckPlayerDicOrWaitDicDownloadHead(playerImg, item);
                }

                for (int j = 0; j < dr.playerList.Count; j++)
                {
                    if (dr.playerList[j].key == GameData.GetInstance().userID)
                    {
                        if (int.Parse(dr.playerList[j].value) <= 0)
                        {
                            SetText(playerScore, dr.playerList[j].value);
                        }
                        else
                        {
                            SetText(playerScore, "+" + dr.playerList[j].value);
                        }
                    }
                }

                //显示   1MVP 2土豪 3 大鱼
                if (item.Rank > 0)
                {
                    switch (item.Rank)
                    {
                        case 1:
                            CheckPlayerDicOrWaitDicDownloadHead(MVPImg, item);
                            break;
                        case 2:
                            CheckPlayerDicOrWaitDicDownloadHead(tuHaoImg, item);
                            break;
                        case 3:
                            CheckPlayerDicOrWaitDicDownloadHead(daYuImg, item);
                            break;
                    }
                }
                else
                {
                    var temp = TooL.LoadPrefab(playerGroup, "GameEndPageItem");
                    temp.GetComponent<GameEndItemPage>().InitGameEndItem(item);
                }
        }
    }

    private void CheckPlayerDicOrWaitDicDownloadHead(GameObject img, DissolveRoomPlayerInfo dr)
    {
        if (dr.LocationIndex == -1)
        {
            DownloadHeadTexture(img.GetComponent<RawImage>(), GameRoomModel.GetInstance().waitPlayerDic[dr.userId.ToString()].avatar);
        }
        else
        {
            DownloadHeadTexture(img.GetComponent<RawImage>(), GameRoomModel.GetInstance().playerDic[dr.LocationIndex].avatar);
        }
    }

    //返回大厅
    private  void GoToGameHallClick()
    {
        if (GameObject.Find("GameRoomPage(Clone)"))
        {
            GameController.GetInstance().DeleteAllExceptGameHall();
        }
        else
        {
            Debug.Log("没有找到战斗预设体");
        }
    }
}
         